package com.enerccio.commonJ.objects;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.enerccio.commonJ.objects.Condition.ConditionException;

/**
 * This class represents the package object in lisp, which stores the symbols
 * @author enerccio
 *
 */
public final class commonJPackage extends commonJObject {
	
	private Map<String, Symbol> packageSymbols;
	private List<commonJPackage> usedPackages;
	private String packageName;
	
	public commonJPackage(String name){
		packageName = name;
		packageSymbols = new HashMap<String, Symbol>();
		usedPackages = new LinkedList<commonJPackage>();
	}
	
	public synchronized String getName(){
		return packageName;
	}
	
	public synchronized Symbol get(String name) throws ConditionException{
		Symbol s = packageSymbols.get(name);
		
		if (s != null)
			return s;
		
		for (commonJPackage used : usedPackages)
			if ((s = used.get(name)) != null)
				return s;

		new Condition("Symbol not found"); /* This fucking throws every time ... */
		return null; // w t f
	}

	public synchronized void addSymbol(String name, Symbol symbol){
		packageSymbols.put(name, symbol);
	}

	public void addUsedPackage(commonJPackage pack){
		usedPackages.add(pack);
	}
	
}
